package com.kgc.dao.impl;

import com.kgc.dao.BaseDao;
import com.kgc.dao.CategoryDao;

import java.sql.ResultSet;

/**
 * <h3>项目名称:demo03050</h3>
 * <p>本API描述:新闻分类数据操作实现类</p>
 * <p>@author : AliyZhang</p>
 * <p>@date : 2021-03-05 19:41</p>
 **/
public class CategoryDaoImpl extends BaseDao implements CategoryDao {
    @Override
    public int queryNewsCountByCid(int cid) {
        //SQL语句
        String sql="select count(id) from news_detail where categoryId=?";
        //参数数组
        Object[] params=new Object[1];
        params[0]=cid;
        ResultSet resultSet = executeQuery(sql, params);
        //声明变量，接收news新闻条数
        int count=0;
        try {
            while (resultSet.next()){
                count=resultSet.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int deleteNewsByCid(int cid) {
        //根据新闻分类删除新闻
        //SQL语句
        String sql="delete from news_detail where categoryId=?";
        //参数数组
        Object[] params=new Object[1];
        params[0]=cid;
        //调用通用的增删改
        return executeUpdate(sql,params);
    }

    @Override
    public int deleteCategory(int cid) {
        //sql字符串
        String sql="delete from news_category where id=?";
        //参数数组
        Object[] params=new Object[1];
        params[0]=cid;
        //调用通用的增删改方法
        return executeUpdate(sql,params);
    }
}
